import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Main from '@/components/Main'
import Role from '@/components/role/role'
import User from '@/components/user/user'

Vue.use(Router)

let router =  new Router({
  routes: [
    {
      path: '/login',
      name: '登陆',
      component: Login
    },
     {
      path: '/main',
      name: '主页',
      component: Main,
      children:[
      	{path: '/user', component: User, name: '用户列表'},
      	{path: '/role', component: Role, name: '角色列表'}
      	
      ]
      	
      
    }
  ]
})


router.beforeEach((to, from, next) => {
  // console.log('to:' + to.path)
  if (to.path.startsWith('/login')) {
    window.localStorage.removeItem('access-token')
    next()
  } else if (to.path.startsWith('/index')) {
    next()
  } else {
    let user = window.localStorage.getItem('access-token');
    if (!user) {
      next({path: '/login'})
    } else {
      next()
    }
  }
})

export default router